/**
 * ymm56.com Inc.
 * Copyright (c) 2013-2019 All Rights Reserved.
 */
package com.garlicts.excel.util;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * Excel工具类
 * @author wangwei
 * @version $Id: ExcelOperator.java, v 0.1 2019-03-27 9:15 user Exp $$
 */
public class ExcelOperator {

    /**
     *	获取行对象
     *	@param rowNum
     */
    public static Row getRow(Sheet sheet, int rowNum) {
        return sheet.getRow(rowNum);
    }

    /**
     *	获取单元格值
     *	@param row 行
     *	@param cellNum 单元格列号
     */
    public static Object getCellValue(Row row, int cellNum) {

        Object val = null;

        Cell cell = row.getCell(cellNum);

        if(cell == null || Objects.equals(cell.getCellType(), Cell.CELL_TYPE_BLANK)) {
            return null;
        }

        // 如果是数字保留两位小数点
        if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {

//				double numericCellValue = cell.getNumericCellValue();
//				DecimalFormat decimalFormat = new DecimalFormat("#.##");
//				val = decimalFormat.format(numericCellValue);

            if(HSSFDateUtil.isCellDateFormatted(cell)) {
                double numericCellValue = cell.getNumericCellValue();
                Date date = HSSFDateUtil.getJavaDate(numericCellValue);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                val = simpleDateFormat.format(date);
            }else {

                double numericCellValue = cell.getNumericCellValue();
                DecimalFormat decimalFormat = new DecimalFormat("#.######");
                val = decimalFormat.format(numericCellValue);

            }

        }else if(cell.getCellType() == Cell.CELL_TYPE_STRING) {

            val = cell.getStringCellValue();

        }else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {

            val = cell.getCellFormula();

        }else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {

            val = cell.getBooleanCellValue();

        }else if(cell.getCellType() == Cell.CELL_TYPE_BLANK) {

            val = null;

        }else if(cell.getCellType() == Cell.CELL_TYPE_ERROR) {

            val = null;

        }else {

            val = cell.getStringCellValue();

        }

        return val;

    }

    /**
     *	获取最后一个数据列号
     */
    public static int getLastDataCellNum(Sheet sheet, int headerNum) {
        return getRow(sheet, headerNum).getLastCellNum();
    }

    /**
     *	获取数据行号
     */
    public static int getDataRowNum(int headerNum) {
        return headerNum + 1;
    }

    /**
     *	获取最后一个数据行号
     */
    public static int getLastDataRowNum(Sheet sheet) {

        return sheet.getLastRowNum();

    }

}
